﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeNess
{
    public partial class Form1 : Form
    {
        const string recordpath = "records.xml";
        const double rate = 19.03;
        const int cutoff = 40;
        const double overtimeRate = rate * 1.5;
        List<timeRecord> records = new List<timeRecord>();

        public Form1()
        {
            InitializeComponent();
            System.IO.FileInfo fi = new System.IO.FileInfo(recordpath);
            if (fi.Exists) //read it in
            {
                    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(records.GetType());
                    System.IO.FileStream fs = new System.IO.FileStream(recordpath,System.IO.FileMode.Open);
                    records = (List<timeRecord>) xs.Deserialize(fs);
                    fs.Close();
            }
            doClock();
        }
        void doClock()
        {
            if (clockedIn()) this.btnClock.Text = "Clock Out";
            else this.btnClock.Text = "Clock In";
        }
        bool clockedIn()
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(records.GetType());
            System.IO.FileStream fs = new System.IO.FileStream(recordpath, System.IO.FileMode.Create);
            xs.Serialize(fs, records);
            fs.Close();
            if (this.records.Count == 0) return false;
            if (this.records[this.records.Count - 1].clockOut.Year ==1) return true;
            else return false;
        }

        private void btnClock_Click(object sender, EventArgs e)
        {
            if (clockedIn()) this.records[this.records.Count - 1].clockOut = DateTime.Now;
            else
            {
                timeRecord t = new timeRecord();
                t.clockIn = DateTime.Now;
                this.records.Add(t);
            }
            doClock();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            double hoursToday=0;
            double earnedToday = 0;
            double hoursThisWeek = 0;
          
            DateTime weekStart = DateTime.Now;
            while (weekStart.DayOfWeek != DayOfWeek.Monday)
            {
                weekStart = weekStart.AddDays(-1);
            }
            weekStart = new DateTime(weekStart.Year, weekStart.Month, weekStart.Day);
            foreach (timeRecord r in records)
            {
                if (r.clockIn.Date == DateTime.Now.Date) hoursToday += r.hours;
                if (r.clockIn.Date >= weekStart) hoursThisWeek += r.hours;
            }
            double hoursUntilOvertime = cutoff - hoursThisWeek;

            double earnedThisWeek = 0;
            if (hoursUntilOvertime < 0)
            {
                earnedThisWeek += cutoff * rate;
                earnedThisWeek += (hoursThisWeek - cutoff) * overtimeRate;
                double ovrTimeToday = Math.Min((-1 * hoursUntilOvertime), hoursToday);
                double normalTimeToday = hoursToday - ovrTimeToday;
                earnedToday += (normalTimeToday) * rate + ovrTimeToday * overtimeRate;
            }
            else
            {
                earnedThisWeek += (hoursThisWeek * rate);
                earnedToday += hoursToday * rate;
            }
            lblHoursToday.Text = Math.Round(hoursToday, 2).ToString();
            lblHoursThisWeek.Text = Math.Round(hoursThisWeek,2).ToString();
            lblHoursUntilOvertime.Text = Math.Round(hoursUntilOvertime,2).ToString();
            lblEarnedThisWeek.Text  = Math.Round(earnedThisWeek,2).ToString();
            lblEarnedToday.Text  = Math.Round(earnedToday,2).ToString();
            this.Text = Math.Round(earnedThisWeek, 2).ToString();

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<DateTime, double> report = new Dictionary<DateTime, double>();
            foreach (timeRecord r in records)
            {
                DateTime day = r.clockIn.Date;
                if (report.ContainsKey(day) )report[day] += r.hours;
                else report.Add(day,r.hours);
            }
            System.IO.FileStream fs = new System.IO.FileStream("out.log",System.IO.FileMode.Create);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
            foreach (KeyValuePair<DateTime, double> kvp in report)
            {
                sw.Write(kvp.Key);
                sw.Write(":");
                sw.Write(kvp.Value);
                sw.Write("\n");
            }
            sw.Close();

        }


    }
}
